<template>
  <el-form-item :prop="form.type.includes('upload')?null:form.model" :label="form.label">
    <el-radio-group v-if="form.type === 'radio'" v-model="inputValue">
      <el-radio v-for="(item, index) in form.radio" :key="index" :label="item.value">{{item.label}}</el-radio>
    </el-radio-group>
    <template v-else-if="form.type === 'select'">
    <el-select v-model="inputValue" :placeholder="form.label">
      <el-option v-for="(item, index) in form.select" :key="index" :value="item.value" :label="item.label"></el-option>
    </el-select>
    </template>
    <template v-else-if="form.type === 'upload_1'">
      <el-upload
        class="upload-demo"
        ref="upload"
        action
        :file-list="fileList"
        :on-change="handleChange"
        :on-remove="handleRemove"
        :auto-upload="false">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <div slot="tip" class="el-upload__tip">{{form.tip}}</div>
      </el-upload>
    </template>
    <template v-else-if="form.type === 'upload_2'">
      <el-upload
        class="avatar-uploader"
        action
        :on-change="handleChange"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </template>
    <el-input v-else :type="form.type" v-model="inputValue" :placeholder="form.label"></el-input>
  </el-form-item>
</template>

<script>
export default {
  name: 'ZForm',
  props: ['form', 'value', 'img'],
  watch: {
    inputValue (value, oldValue) {
      this.$emit('input', value)
    },
    value (value) {
      this.inputValue = value
    },
    img (value) {
      this.imageUrl = value
    }
  },
  data () {
    return {
      inputValue: '',
      fileList: [],
      imageUrl: null,
      isInsert: 1
    }
  },
  methods: {
    handleChange (file, fileList) {
      this.fileList = fileList.slice(-1)
      if (!this.isInsert) {
        return
      }
      this.$emit('input', file.raw)
      this.imageUrl = window.URL.createObjectURL(file.raw)
    },
    handleRemove (file, fileList) {
      this.$emit('input', null)
    },
    handleAvatarSuccess (res, file) {
      this.imageUrl = window.URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload (file) {
      if (!file.type.includes('image')) {
        this.$error('只能插入图片')
        this.isInsert = 0
        return
      }
      this.isInsert = 1
      this.imageUrl = window.URL.createObjectURL(file.raw)
    },
    initImageUrl () {
      this.imageUrl = this.img
    }
  },
  created () {
    this.inputValue = this.value
    this.initImageUrl()
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
